﻿#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using AbstractClass.Extensions;
using AbstractClass.IO;
using AbstractClass.Linq;
using NUnit.Framework;

namespace QuasarTest.Win32
{
    [TestFixture]
    public class ExtensionTest
    {
        [Test]
        public void TestControlExtensions()
        {
            var form = new Form {Name = "Form"};
            var groupBox = new GroupBox {Name = "GroupBox"};
            var panel = new Panel {Name = "Panel"};
            var internalPanel = new Panel {Name = "InternalPanel"};
            var button1 = new Button {Name = "Button1"};
            var button2 = new Button {Name = "Button2"};
            var button3 = new Button {Name = "Button3"};
            var button4 = new Button {Name = "Button4"};
            var button5 = new Button {Name = "Button5"};
            var button6 = new Button {Name = "Button6"};

            form.Controls.Add(groupBox);
            form.Controls.Add(button3);
            form.Controls.Add(button4);
            groupBox.Controls.Add(button2);
            groupBox.Controls.Add(panel);
            panel.Controls.Add(button1);
            button1.Controls.Add(button5);
            button4.Controls.Add(button6);
            button6.Controls.Add(internalPanel);

            IEnumerable<Panel> panels = form.GetControlsOfType<Panel>();
            panels.Process(Assert.IsInstanceOf<Panel>);

            foreach (Panel panelChild in panels)
            {
                Console.WriteLine(panelChild.Name);
            }


            IEnumerable<Button> buttons = form.GetControlsOfType<Button>();
            buttons.Process(Assert.IsInstanceOf<Button>);

            foreach (Button button in buttons)
            {
                Console.WriteLine(button.Name);
            }

            form.GetChildControls().Process(node => Console.WriteLine(node.Name));
        }

        [Test]
        public void TestIOExtensions()
        {
            string directoryPath = Path.GetTempPath();
            var dirInfo = new DirectoryInfo(directoryPath);
            IEnumerable<FileSystemInfo> items = dirInfo.GetFileSystemInfos(SearchOption.AllDirectories);

            Assert.Greater(items.Count(), 0);
            Assert.IsTrue(items.OfType<FileInfo>().Count() > 0);
            Assert.IsTrue(items.OfType<DirectoryInfo>().Count() > 0);
            Assert.IsInstanceOf<FileInfo>(dirInfo.GetFiles(SearchOption.AllDirectories).First());
            Assert.IsInstanceOf<DirectoryInfo>(dirInfo.GetDirectories(SearchOption.TopDirectoryOnly).First());
            Console.WriteLine(items.Count());

            IEnumerable<FileInfo> temp = dirInfo.FindInFiles("a", true, SearchOption.TopDirectoryOnly, "*.log");
            temp.Process(x => Console.WriteLine(x.FullName));
            Assert.Greater(temp.Count(), 0);

            DirectoryInfo delDir = dirInfo.CreateSubdirectory("TestToDelete");
            temp.Process(x => x.CopyTo(delDir.FullName + "\\" + x.Name));
            Assert.IsTrue(delDir.Exists);
            Assert.Greater(delDir.GetFiles().Length, 0);
            Assert.IsTrue(delDir.DeleteAllContent());
            Assert.IsTrue(delDir.Exists);

            FileInfo file = temp.First();
            file.SendToRecycleBin();
            Assert.IsFalse(file.Exists);
            delDir.SendToRecycleBin();
            Assert.IsFalse(Directory.Exists(delDir.FullName));

            using (var fStream = new FileStream(temp.Last().FullName, FileMode.Open))
            {
                Console.WriteLine(fStream.GetChecksum(ChecksumType.SHA1));
                Console.WriteLine(fStream.GetChecksum(ChecksumType.CRC32));
                Console.WriteLine(fStream.GetChecksum(ChecksumType.MD5));
            }

            //TODO: Copy, Move Files to a Directory name rather specifying a destination file name
        }

        [Test]
        public void TestImageExtensions()
        {
            Bitmap bmp;
            var odlg = new OpenFileDialog
                           {
                               Filter = "Image File (*.bmp, *.jpg, *.png)|*.bmp;*.png;*.jpg"
                           };

            if (odlg.ShowDialog() == DialogResult.OK)
            {
                bmp = (Bitmap) Image.FromFile(odlg.FileName);
                int[,] array = bmp.ToArray();
                Assert.AreEqual(array.LongLength, bmp.Height * bmp.Width);

                var random = new Random();
                int i = random.Next(bmp.Width);
                int j = random.Next(bmp.Height);

                Assert.AreEqual(Color.FromArgb(array[i, j]), bmp.GetPixel(i, j));
                Color[,] colors = bmp.ToColorArray();
                Assert.AreEqual(colors[i, j].ToArgb(), array[i, j]);

                Bitmap cloneBmp = bmp.Clone(bmp.PixelFormat);
                Assert.AreEqual(cloneBmp.PixelFormat, bmp.PixelFormat);

                Bitmap grayBmp = cloneBmp.ToGrayScale();
                Assert.IsTrue(grayBmp.PixelFormat == PixelFormat.Format8bppIndexed);

                var sdlg = new SaveFileDialog
                               {
                                   Filter = "Image File (*.bmp, *.jpg, *.png)|*.bmp;*.png;*.jpg"
                               };

                if (sdlg.ShowDialog() == DialogResult.OK)
                {
                    grayBmp.Save(sdlg.FileName);
                }
            }
        }
    }
}